import {
  getTerm,
} from "../../pages/tables/helper.js"
import {
  hasToLogin,
  setStore,
  getStore
} from "../../utils/util.js"
import {
  sortTime
} from "./helper.js"
import {
  getExam,
  getGrade
} from '../../utils/api.js'

Page({

  data: {
    exam: [],
    grade: [],
    selectorIsLeft: true,
    gradeDetail: undefined,
    // 实际用于显示的Picker
    yearPicker: [2020, 2019, 2018, 2017],
    termPicker: [1, 2],
    // 每学期维护
    selectYear: 2020,
    selectTerm: 1,
    // 成绩和考试信息的Picker不同，成绩可以查看全部
    examYearPicker: [2020, 2019, 2018, 2017],
    examTermPicker: [1, 2],
    examYear: 2020,
    examTerm: 1,
    gradeYearPicker: ["全部", 2020, 2019, 2018, 2017],
    gradeTermPicker: ["全部", 1, 2],
    gradeYear: 2020,
    gradeTerm: 1,
    showSetModel: false,
    // 学年和学期对应的请求参数
    yearParam: {
      "全部": '',
      2020: 2020,
      2019: 2019,
      2018: 2018,
      2017: 2017
    },
    termParam: {
      "全部": '',
      1: 3,
      2: 12,
      3: 16
    }
  },

  onLoad: function (options) {
    // check token
    hasToLogin()
    getStore('exam').then(data => {
      this.setData({
        exam: data.data
      })
    },
    reason => {
      console.log(reason)
    })
    getStore('grade').then(data => {
      this.setData({
        grade: data.data
      })
    },
    reason => {
      console.log(reason)
    })
    const {
      selectTerm,
      selectYear
    } = this.data
    this.fetchExam(selectYear, selectTerm)
    this.fetchGrade(selectYear, selectTerm)
  },
  toggleSelector: function () {
    const {
      selectorIsLeft
    } = this.data
    this.setData({
      selectorIsLeft: !selectorIsLeft
    })
    if (this.data.selectorIsLeft) {
      // 考试
      this.setData({
        yearPicker: this.data.examYearPicker,
        termPicker: this.data.examTermPicker,
        selectYear: this.data.examYear,
        selectTerm: this.data.examTerm
      })
    } else {
      // 成绩
      this.setData({
        yearPicker: this.data.gradeYearPicker,
        termPicker: this.data.gradeTermPicker,
        selectYear: this.data.gradeYear,
        selectTerm: this.data.gradeTerm
      })
    }
  },

  passed(grade) {
    // grade为字符串 '80' == 80; '良好' != NaN
    return grade == parseInt(grade) && parseInt(grade) < 60
  },

  showSetModal() {
    const showSetModal = true
    this.setData({
      showSetModal
    })
  },


  cancelModal() {
    const showSetModal = false
    this.setData({
      showSetModal
    })
  },

  changeYear(e) {
    let selectYear = this.data.yearPicker[e.detail.value]
    if (this.data.selectorIsLeft) {
      // 考试信息
      console.log(selectYear)
      this.setData({
        selectYear: selectYear,
        examYear: selectYear
      })
    } else {
      // 成绩信息
      this.setData({
        selectYear: selectYear,
        gradeYear: selectYear
      })
    }
  },

  changeTerm(e) {
    let selectTerm = this.data.termPicker[e.detail.value]
    if (this.data.selectorIsLeft) {
      this.setData({
        selectTerm: selectTerm,
        examTerm: selectTerm
      })
    } else {
      this.setData({
        selectTerm: selectTerm,
        gradeTerm: selectTerm
      })
    }
  },

  selectTable() {
    const {
      selectYear,
      selectTerm
    } = this.data
    if (selectYear === 0 || selectTerm === 0) {
      wx.showToast({
        title: '请选择学期',
      })
    } else if (this.data.selectorIsLeft) {
      // 查询考试
      this.fetchExam(selectYear, selectTerm)
    } else {
      // 查询成绩
      this.fetchGrade(selectYear, selectTerm)
    }
    this.cancelModal()
  },

  fetchExam(selectYear, selectTerm) {
    selectYear = this.data.yearParam[selectYear]
    selectTerm = this.data.termParam[selectTerm]
    return getExam({
      xnm: selectYear,
      xqm: selectTerm
    }).then(res => {
      // name room time
      let data = res.data.data.items
      let exam = []
      data.forEach((item) => {
        let obj = {}
        obj.name = item.kcmc
        obj.room = item.cdmc
        obj.time = item.kssj
        exam.push(obj)
      })
      exam = sortTime(exam)
      this.setData({
        exam: exam
      })
      setStore({
        exam: exam
      })
    })
  },

  fetchGrade(selectYear, selectTerm) {
    selectYear = this.data.yearParam[selectYear]
    selectTerm = this.data.termParam[selectTerm]
    return getGrade({
      xnm: selectYear,
      xqm: selectTerm
    }).then(res => {
      // grade name gpa
      // 平时成绩 期末 总成绩
      let data = res.data.data.items
      // console.log(res.data)
      let grade = new Map()
      data.forEach((item) => {
        if (grade.has(item.kch)) {
          grade.get(item.kch).gradeDetail.push([item.xmblmc, item.xmcj])
        } else  {
          let obj = {}
          obj.gradeDetail = [[item.xmblmc, item.xmcj]]
          obj.name = item.kcmc
          grade.set(item.kch, obj)
        }
        if (item.xmblmc === "总评") {
          grade.get(item.kch).grade = item.xmcj
          grade.get(item.kch).fail = this.passed(item.xmcj)
        }
      })
      grade = [...grade.values()]
      this.setData({
        grade: grade
      })
      setStore({
        grade: grade
      })
    })
  }
})